﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Configuration;
using System.IO;
using System.Data;

public partial class mwc_admin_controls_filemanager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        thePathUpload.Text = "~/";
        loadDirectoryInfo();
        loadFile("");
    }
    protected void loadDirectoryInfo()
    {
        DirectoryInfo di = new DirectoryInfo(ConfigurationSettings.AppSettings["upload_path"]);
        TreeNode RootNode = OutputDirectory(di,null);
        tvFolder.Nodes.Add(RootNode);
    }
    protected void uploadComplete(object sender, AsyncFileUploadEventArgs e)
    {
        if (AsyncFileUpload1.HasFile)
        {
            AsyncFileUpload1.SaveAs(ConfigurationSettings.AppSettings["upload_path"] + AsyncFileUpload1.FileName);
            //loadFile("");
        }
    }

    protected void loadFile(string FolderName)
    {
        string allPath = ConfigurationSettings.AppSettings["upload_path"] + FolderName;
        try
        {
            DirectoryInfo _di = new DirectoryInfo(allPath);
            FileInfo[] Files = _di.GetFiles();
            DataTable dt = new DataTable();

            DataColumn dCol1 = new DataColumn("FileName", typeof(System.String)); // string FirstC=”column1″
            DataColumn dCol2 = new DataColumn("No", typeof(System.String)); // string SecondC=”column2″
            DataColumn dCol3 = new DataColumn("imageURL", typeof(System.String)); 
            //dt.Columns.Add(dCol2);
            //dt.Columns.Add(dCol1);
            dt.Columns.Add(dCol3);
            

            for (int FileCount = 0; FileCount < Files.Length; FileCount++)
            {
                //DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));
                DataRow dr = dt.NewRow();
                //dr["No"] = (FileCount + 1).ToString();
                //dr["FileName"] = Files[FileCount].Name;
                //System.Web.UI.HtmlControls.HtmlImage hi = new System.Web.UI.HtmlControls.HtmlImage();
                //hi.Src = "http://server18813852168.internet-server.dk/components/com_hdflvplayer/videos/1_thumb.jpg";
                //dr["Image"] = hi;
                string imageURL = "";
                if (FolderName != "")
                {
                    imageURL = ConfigurationSettings.AppSettings["image_root_path"] + FolderName + "/" + Files[FileCount].Name;
                }
                else
                {
                    imageURL = ConfigurationSettings.AppSettings["image_root_path"] + Files[FileCount].Name;
                }


                dr["imageURL"] = ResolveUrl(imageURL);
                dt.Rows.Add(dr);
            }

            /*foreach (DataColumn col in dt.Columns) { 
                BoundField bField = new BoundField();
                bField.DataField = col.ColumnName;
                bField.HeaderText = col.ColumnName;

                TemplateField tf = new TemplateField();
                tf.HeaderText = col.ColumnName;

                gvFile.Columns.Add(tf);
                gvFile.Columns.Add(bField);
            }*/
            gvFile.DataSource = dt;
            gvFile.DataBind();

        }
        catch (Exception eloadFile) { 
            
        }
    }

    TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)
    {
        // validate param
        if (directory == null) return null;

        // create a node for this directory
        TreeNode DirNode = new TreeNode(directory.Name);

        // get subdirectories of the current directory
        System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();

        // output each subdirectory
        for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)
        {
            OutputDirectory(SubDirectories[DirectoryCount], DirNode);
        }

        // output the current directories files
        System.IO.FileInfo[] Files = directory.GetFiles();

        for (int FileCount = 0; FileCount < Files.Length; FileCount++)
        {
            
            //DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name));
        }

        // if the parent node is null, return this node
        // otherwise add this node to the parent and return the parent
        if (parentNode == null)
        {
            return DirNode;
        }
        else
        {
            parentNode.ChildNodes.Add(DirNode);
            return parentNode;
        }
    }
    
}
